<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>The Ajax 02 Page</h1>
    <button onclick="doAjaxGet()">Do Ajax Get</button>
    <button onclick="doAjaxPost()">Do Ajax Post</button>
    <button onclick="doAjaxDelete()">Do Ajax Delete</button>
    <button onclick="doAjaxPut()">Do Ajax Put</button>
    <div id="result"></div>
<script>
    //更新
    function doAjaxPut(){
        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange=function (){
            if(xhr.readyState==4&&xhr.status==200){
                document.getElementById("result").innerHTML
                    =xhr.responseText;
            }
        }
        xhr.open("Put","http://localhost/doAjaxPut",true);
        //put请求向服务端传递数据,需要设置请求头,必须在open之后
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        //发送请求(put请求传递数据,需要将数据写入到send方法内部)
        xhr.send("id=101&name=hani&remark=Computer...");

    }
    function  doAjaxDelete(){
        //创建xhr对象
        let xhr=new XMLHttpRequest();
        //设置状态监听
        xhr.onreadystatechange=function () {
            if(xhr.readyState==4&&xhr.status==200){
                document.getElementById("result").innerHTML
                    =xhr.responseText;
            }

        }
        //建立连接
        let params="100";
        xhr.open("delete",`http://localhost/doAjaxDelete/${params}`,true);
        //发送请求
        xhr.send(null);
    }
    function  doAjaxGet(){
       //创建xhr对象
        let xhr=new XMLHttpRequest();
        //设置状态监听
        xhr.onreadystatechange=function () {
            if(xhr.readyState==4&&xhr.status==200){
                document.getElementById("result").innerHTML
                    =xhr.responseText;
            }

        }
        //建立连接
        let params="";
        xhr.open("Get",`http://localhost/doAjaxGet/${params}`,true);
        //发送请求
        xhr.send(null);
    }
    function doAjaxPost(){
        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange=function (){
            if(xhr.readyState==4&&xhr.status==200){
                document.getElementById("result").innerHTML
                    =xhr.responseText;
            }
        }
        xhr.open("POST","http://localhost/doAjaxPost",true);
        //post请求向服务端传递数据,需要设置请求头,必须在open之后
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        //发送请求(post请求传递数据,需要将数据写入到send方法内部)
        xhr.send("id=101&name=Computer&remark=Computer...");

    }
</script>
</body>
</html>